package cs236369.webserver;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultHttpServerConnection;

/**
 * A class that handles general errors the server detected.
 * @author Galus
 *
 */
public class ErrorHandler {
	/**
	 * Generates an HTML string to be sent to a client when an error occured.
	 * @param message - the message that the programmer choose to share.
	 * @param errorCode - the http reason code.
	 * @param reason - the http reason phrase.
	 * @return
	 */
	private String errorString(String message, int errorCode, String reason){
		String page = 
			"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
			"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" +
			"<head>" +
			"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
			"<title>" + " HttpResponse: " + errorCode + "-" + reason + "</title> " +
			"</head>" +
			"<body>" +
			"<h1> The Server Responded With The Following Message</h1>" +
			message +
			"</body>" +
			"</html>";
			
		return page;
	}
	
	/**
	 * This function will send an error page to the client.
	 * @param response - the http response to be sent.
	 * @param message - the message that the programmer choose to share.
	 * @param connToClient - the connection to the client/
	 * @throws HttpException
	 * @throws IOException
	 */
	public void handle(HttpResponse response, String message, DefaultHttpServerConnection connToClient)
	throws HttpException, IOException {
		
		
		String errorStr = errorString(message, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
		StringEntity entity = new StringEntity(errorStr);
		response.setEntity(entity);

		String contentLength = Long.toString(entity.getContentLength());
		response.addHeader("Content-length",contentLength);
				
		connToClient.sendResponseHeader(response);
		connToClient.sendResponseEntity(response);
					
	}
	/**
	 *Generate a page with the list of files contained in the given path, and sends it to the client.
	 * @param response
	 * @param path
	 * @param connToClient
	 * @throws HttpException
	 * @throws IOException
	 */
	public void handleDirectory(HttpResponse response, String path, DefaultHttpServerConnection connToClient)
	throws HttpException, IOException {
		
		
		File file = new File(path);
		
		String[] filesList = file.list();
		StringBuilder fileListBuilder =  new StringBuilder("<h2>You Have Requested a Directory. List:\n <br/> </h2> <ol>");
		for(String s : filesList){
			int slashLoc = path.lastIndexOf(File.separatorChar);
			String parent = path.substring(slashLoc + 1);
			fileListBuilder.append("<li>" + "<a href=\"" + parent + "/" + s + "\">"+ s +"</a>" + "</li>");
		}
		fileListBuilder.append("</ol>");
		
		String errorStr = errorString(fileListBuilder.toString(), 
				response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
		StringEntity entity = new StringEntity(errorStr);
		response.setEntity(entity);
		
		String contentLength = Long.toString(entity.getContentLength());
		response.addHeader("Content-length",contentLength);


		connToClient.sendResponseHeader(response);
		connToClient.sendResponseEntity(response);
						
	}
	
	
		
	
	
}
